#include "Polygon.h"
// synchro
namespace HW2
{

	Polygon::Polygon(Color color) : color(color), filled(false)
	{
		k.r = color.r/255;
		k.g = color.g/255;
		k.b = color.b/255;

		if ((k.r==0)&&(k.g==0)&&(k.b==0))
			k.b= 1;

		ks.r = 1;
		ks.g = 1;
		ks.b = 1;
		/*ks.r = k.r;
		ks.g = k.g;
		ks.b = k.b;*/

		for(unsigned int i = 0; i < coordinates.size() ; i ++){  
			coordinatesColor.push_back(0);
		}

	}


	void Polygon::addCoordinates(Coordinates &c)
	{
		coordinates.push_back(c);
		normals.push_back(Normal()); // Normal does not exists
	}
	void Polygon::addCoordinatesWithNormal(Coordinates &c, Coordinates &normal)
	{
		coordinates.push_back(c);
		normals.push_back(Normal(normal));
	}

	void Polygon::setNormal(Coordinates &c)
	{
		normal = Normal(c);
	}
	
	void Polygon::computeNormal()
	{
		if(coordinates.size() < 3)
		{
			normal = Normal(); // cannot compute normal
			return;
		}
		Vector A = coordinates[1] - coordinates[0];
		Vector B = coordinates[2] - coordinates[1];
		Vector cross = A.cross(B);
		if(cross.norm() == 0)
			normal = Normal(); // cannot compute normal
		else
			normal = Normal(cross);
	}

	Coordinates Polygon::center()
	{
		Coordinates r;
		for(unsigned i = 0; i < coordinates.size(); ++i)
		{
			r+= coordinates[i];
		}
		r = r / coordinates.size();
		return r;
	}
}